<template>
  <div class="age-box">
    <div class="title">
      <p>年龄比例</p>
      <p class="bg" />
    </div>
    <div ref="charts" class="echarts-box" />
  </div>
</template>

<script setup lang="ts" >
import { onMounted, ref } from 'vue';
import * as echarts from 'echarts';

let charts = ref();

onMounted(() => {
  let mycharts = echarts.init(charts.value);
  let option = {
    title: {
      text: '本日总数',
      left: '95',
      top: 'center',
      textStyle: {
        color: 'white',
        fontSize: 15,
      },
    },
    tooltip: {
      trigger: 'item',
    },
    legend: {
      right: 30,
      top: 40,
      orient: 'vertical', //图例组件方向的设置
      textStyle: {
        color: 'white',
        fontSize: 14,
      },
      itemWidth: 20,
      itemHeight: 20,
      formatter: function (name: any) {
        const total = 1048 + 735 + 580 + 484 + 300 + 300;
        const dataItem: any = option.series[0].data.find((item) => item.name === name);
        const percentage = ((dataItem.value / total) * 100).toFixed(2);
        return `${name}: ${percentage}%`;
      },
    },
    series: [
      {
        name: '今日总数',
        type: 'pie',
        radius: ['40%', '70%'],
        center: ['30%', '50%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 10,
        },
        label: {
          show: true,
          formatter: '{d}%',
          position: 'inside',
          color: 'white',
        },

        labelLine: {
          show: false,
        },
        data: [
          { value: 1048, name: '10岁以下' },
          { value: 735, name: '10-18岁' },
          { value: 580, name: '18-30岁' },
          { value: 484, name: '30-40岁' },
          { value: 300, name: '40-60岁' },
          { value: 300, name: '60岁以上' },
        ],
      },
    ],
    //调整图形图标的位置
    grid: {
      left: 0,
      top: 0,
      right: 0,
      bottom: 0,
    },
  };
  mycharts.setOption(option);
});
</script>

<style lang="less" scoped>
.age-box {
  background: url('@/assets/images/screen/dataScreen-main-lb.png') no-repeat;
  background-size: 100% 100%;
  .title {
    color: white;
    font-size: 20px;
    .bg {
      width: 68px;
      height: 7px;
      background: url('@/assets/images/screen/dataScreen-title.png') no-repeat;
      background-size: 100% 100%;
      margin-top: 10px;
    }
  }
  .echarts-box {
    height: 260px;
  }
}
</style>
